package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seawaterbt.ssm.entity.*;
import com.seawaterbt.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZDatHeatStation {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    CzDatHeatstationdataService heatstationdataService;
    @Autowired
    CzBasHeatstationService basHeatstationService;
    @Autowired ZDatHeatStationDataService zDatHeatStationDataService;
    @Autowired CzDatHeatstationdataHistoryService czDatHeatstationdataHistoryService;
    public void sendData() {
        QueryWrapper<CzDatHeatstationData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("heatstation_code", 130902008).or().likeRight("heatstation_code", 130903007);
        Date now = new Date();
        List<CzDatHeatstationData> czDatHeatstationData = heatstationdataService.list(queryWrapper);
        for (CzDatHeatstationData czDatCrew : czDatHeatstationData) {
            CzBasHeatstation one = basHeatstationService.listOne(czDatCrew.getHeatstationCode());
            if (one != null && one.getCodeProvince() != null && !one.getCodeProvince().equals("")) {
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("HeatStationID", one.getCodeProvince());
                wrapper.eq("GetTime", czDatCrew.getGetTime());
                ShengDatHeatStationData byCodeTime = zDatHeatStationDataService.getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    ShengDatHeatStationData shengData = new ShengDatHeatStationData();
                    shengData.setHeatStationId(one.getCodeProvince());
                    shengData.setAddTime(now);
                    shengData.setGetTime(czDatCrew.getGetTime());
                    shengData.setBackPre(Double.valueOf(czDatCrew.getBackPre()));
                    shengData.setBackTemp(Double.valueOf(czDatCrew.getBackTemp()));
                    shengData.setCumulativeHeat(Double.valueOf(czDatCrew.getCumulativeHeat())
                    );
                    shengData.setInstantaneousFlow(Double.valueOf(czDatCrew.getInstantaneousFlow()));
                    shengData.setSupplyPre(Double.valueOf(czDatCrew.getSupplyPre()));
                    shengData.setSupplyTemp(Double.valueOf(czDatCrew.getSupplyTemp()));
                    zDatHeatStationDataService.save(shengData);
                }
            }
        }

        System.out.println("完成同步换热站到中间库 " + df.format(new Date()));
    }

    public void getData(){
        QueryWrapper<ShengDatHeatStationData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("HeatStationID", 130900011).or().likeRight("HeatStationID", 130900013);
        List<ShengDatHeatStationData> shengDatHeatStationDataList = zDatHeatStationDataService.list(queryWrapper);
        for (ShengDatHeatStationData shengDatHeatStationData:shengDatHeatStationDataList){
            CzBasHeatstation one = basHeatstationService.GetOneByProviencecode(shengDatHeatStationData.getHeatStationId());
            if (one != null){
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("heatstation_code", one.getCode());
                wrapper.eq("get_time", shengDatHeatStationData.getGetTime());
                CzDatHeatstationData byCodeTime = heatstationdataService.getByCodeTime(wrapper);
                if (byCodeTime == null){
                    CzDatHeatstationData czDatHeatstationData = new CzDatHeatstationData();
                    czDatHeatstationData.setBackPre(shengDatHeatStationData.getBackPre()+"");
                    czDatHeatstationData.setBackTemp(shengDatHeatStationData.getBackTemp()+"");
                    czDatHeatstationData.setCreateTime(new Date());
                    czDatHeatstationData.setGetTime(shengDatHeatStationData.getGetTime());
                    czDatHeatstationData.setCumulativeHeat(shengDatHeatStationData.getCumulativeHeat()+"");
                    czDatHeatstationData.setHeatstationCode(one.getCode());
                    czDatHeatstationData.setInstantaneousFlow(shengDatHeatStationData.getInstantaneousFlow()+"");
                    czDatHeatstationData.setSupplyPre(shengDatHeatStationData.getSupplyPre()+"");
                    czDatHeatstationData.setSupplyTemp(shengDatHeatStationData.getSupplyTemp()+"");
                    heatstationdataService.saveOrUpdate(czDatHeatstationData);
                    CzDatHeatstationDataHistory czDatHeatstationDataHistory = new CzDatHeatstationDataHistory();
                    czDatHeatstationDataHistory.setBackPre(shengDatHeatStationData.getBackPre()+"");
                    czDatHeatstationDataHistory.setBackTemp(shengDatHeatStationData.getBackTemp()+"");
                    czDatHeatstationDataHistory.setCreateTime(new Date());
                    czDatHeatstationDataHistory.setGetTime(shengDatHeatStationData.getGetTime());
                    czDatHeatstationDataHistory.setCumulativeHeat(shengDatHeatStationData.getCumulativeHeat()+"");
                    czDatHeatstationDataHistory.setHeatstationCode(one.getCode());
                    czDatHeatstationDataHistory.setInstantaneousFlow(shengDatHeatStationData.getInstantaneousFlow()+"");
                    czDatHeatstationDataHistory.setSupplyPre(shengDatHeatStationData.getSupplyPre()+"");
                    czDatHeatstationDataHistory.setSupplyTemp(shengDatHeatStationData.getSupplyTemp()+"");
                    czDatHeatstationdataHistoryService.save(czDatHeatstationDataHistory);
                }
            }
        }
        System.out.println("完成换热站数据拉取"+new Date());
    }

}
